package com.ilxqx.framework.system.entity;

import com.ilxqx.framework.system.entity.anno.Excel;
import com.ilxqx.framework.system.enumeration.SysPermissionMethodEnum;
import lombok.Data;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 系统权限实体类
 * @author venus
 */
@Entity(name = "sys_permission")
@Data
@DynamicInsert
@DynamicUpdate
public class SysPermission implements Serializable {

    private static final long serialVersionUID = -7244169869123589886L;

    /**
     * 权限ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long permissionId;

    /**
     * 父级权限
     */
    @Excel(value = "父级权限")
    private Long parentPermissionId;

    /**
     * 名称
     */
    @Excel(value = "名称")
    private String name;

    /**
     * 是否是规则
     */
    @Excel(value = "是否是规则")
    private Boolean rule;

    /**
     * 请求URI
     */
    @Excel(value = "请求URI")
    private String uri;

    /**
     * 请求方法
     */
    @Excel(value = "请求方法", method = "getMeaning")
    private SysPermissionMethodEnum method;

    /**
     * 备注
     */
    @Excel(value = "备注")
    private String remark;

    /**
     * 操作用户ID
     */
    private Long operationUserId;

    /**
     * 操作用户
     */
    @Excel(value = "操作用户")
    private String operationUserName;

    /**
     * 创建时间
     */
    @Excel(value = "创建时间")
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @Excel(value = "更新时间")
    private LocalDateTime updateTime;

    /**
     * 子权限
     */
    @OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.EAGER, orphanRemoval = true)
    @JoinColumn(name = "parentPermissionId")
    private List<SysPermission> children;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        SysPermission that = (SysPermission) o;

        return new EqualsBuilder()
                .append(permissionId, that.permissionId)
                .append(parentPermissionId, that.parentPermissionId)
                .append(name, that.name)
                .append(rule, that.rule)
                .append(uri, that.uri)
                .append(method, that.method)
                .append(remark, that.remark)
                .append(operationUserId, that.operationUserId)
                .append(operationUserName, that.operationUserName)
                .append(createTime, that.createTime)
                .append(updateTime, that.updateTime)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(permissionId)
                .append(parentPermissionId)
                .append(name)
                .append(rule)
                .append(uri)
                .append(method)
                .append(remark)
                .append(operationUserId)
                .append(operationUserName)
                .append(createTime)
                .append(updateTime)
                .toHashCode();
    }

}